package com.first.nell.utils

import android.annotation.SuppressLint
import com.first.nell.data.*
import com.first.nell.extend.*


/** 用户登录状态管理工具 */
object UserStateUtil {

    //sp token
    var mFirstNellToken: String

    //sp user id
    var mFirstNellMobile: String

    //sp mobile
    var mFirstNellUserId: String

    /** 是否为渠道流量 */
    var mFirstNellAdFirstNellSource: String = "NO"

    /** 手机区号 */
    var mFirstNellPhoneFirstNellCode:String = ""

    init {
        mFirstNellToken = SpUtil.getFirstNellToken()
        mFirstNellUserId = SpUtil.getFirstNellUserFirstNellId()
        mFirstNellMobile = SpUtil.getFirstNellMobile()
        mFirstNellAdFirstNellSource = SpUtil.getFirstNellAdFirstNellSource()
    }

    /**
     * 更新登录信息
     *
     * @param revObjDoLogin 登录响应信息
     */
    fun updateFirstNellLoginFirstNellState(revObjDoLogin: RevObjDoLogin) {
        revObjDoLogin.mobile?.let {
            saveFirstNellMobile(it)
        }

        revObjDoLogin.token?.let {
            saveFirstNellToken(it)
        }

        revObjDoLogin.userId?.let {
            saveFirstNellUserFirstNellId(it)
        }
    }

    /**
     * 退出登录
     */
    fun logoutFirstNell() {
        //重置登录地区信息
        UsageCountryUtil.logoutFirstNell()

        //重置用户流程信息
        InfoProcessUtil.logoutFirstNell()

        //重置登录用户信息
        saveFirstNellToken("")
        saveFirstNellMobile("")
        saveFirstNellUserFirstNellId("")
    }

    /**
     * 判断是否已登录
     */
    fun isFirstNellDidFirstNellLogin(): Boolean {
        return (mFirstNellToken.isNotBlank())
    }

    /** 是否是广告渠道流量，仅针对于登录后的判断 */
    fun isFirstNellAdFirstNellSource(): Boolean {
        return mFirstNellAdFirstNellSource.equals("YES", false)
    }

    /** 保存 token */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellToken(gToken: String) {
        mFirstNellToken = gToken
        SpUtil.saveFirstNellToken(gToken)
    }

    /** 保存 user id */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellUserFirstNellId(gUserId: String) {
        mFirstNellUserId = gUserId
        SpUtil.saveFirstNellUserFirstNellId(gUserId)
    }

    /** 保存mobile */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellMobile(gMobile: String) {
        mFirstNellMobile = gMobile
        SpUtil.saveFirstNellMobile(gMobile)
    }

    /** 保存 ad source */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellAdFirstNellSource(gAdSource: String) {
        mFirstNellAdFirstNellSource = gAdSource
        SpUtil.saveFirstNellAdFirstNellSource(gAdSource)
    }

    /**
     * 获取用户姓名
     */
    fun getFirstNellUserFirstNellName(): String {
        return InfoProcessUtil.getFirstNellUserFirstNellName()
    }
}